import { getData, updateData } from '@vimi/utils-api'
import { isDefined } from '@vimi/utils-tool'
import { getApprove } from '@/credit/mixin/wf'
const relatedTableName = 'customer_related'

export const enumGuaFundsType = {
  1: '一次性收取',
  // 2: '按年收取',
}

export const enumGuaFundsSelfType = {
  1: '一次性收取',
  2: '按年收取',
}

// 评审委员会审批 0未审批
export const enumCommitteeType = {
  1: '同意',
  2: '否决',
  3: '复议',
  4: '有条件同意',
}

// 获取项目信息（关联表）
export const getRelated = relatedId =>
  getData({
    table: relatedTableName,
    params: {
      'customer_related_id.eq': relatedId,
    },
  }).then(res => res?.data?.[0] ?? {})

// 更新关联表信息
export const updateRelatedData = (id, data) =>
  updateData({
    table: relatedTableName,
    id,
    data,
  })

// 判断是否为企业
export const projectIsCompany = projectData => +projectData.customer_type === 1

export const getWfKey = isUnion => (isUnion ? 'PROJECT_APPROVAL_DIR_PRO' : 'PROJECT_APPROVAL')

const getApproveConfig = isUnion =>
  isUnion
    ? {
        'PROJECT_APPROVAL_DIR_PRO-RESA': {
          cmpt: 'approve-related',
          rekey: null,
        },
        'PROJECT_APPROVAL_DIR_PRO-RISK': {
          cmpt: 'approve-risk',
          rekey: null,
        },
        psms: {
          cmpt: 'approve-register',
          rekey: null,
          type: [1, 2],
          dataParse: () => ({
            type: 2,
          }),
        },
      }
    : {
        'PROJECT_APPROVAL-RESA': {
          cmpt: 'approve-related',
          rekey: null,
        },
        'PROJECT_APPROVAL-RESB': {
          cmpt: 'approve-common',
          rekey: 'PROJECT_APPROVAL-RESA',
        },
        'PROJECT_APPROVAL-RES_MANAGE': {
          cmpt: 'approve-common',
          rekey: 'PROJECT_APPROVAL-RESA',
        },
        'PROJECT_APPROVAL-RISK': {
          cmpt: 'approve-risk',
          rekey: 'PROJECT_APPROVAL-RESA',
        },
        'PROJECT_APPROVAL-RISK_MANAGE': {
          cmpt: 'approve-risk-manager',
          rekey: 'PROJECT_APPROVAL-RESA',
        },
        'PROJECT_APPROVAL-JURY': {
          cmpt: 'approve-committee',
          rekey: null,
          type: 1,
        },
        'PROJECT_APPROVAL-SECRETART': {
          cmpt: 'approve-register',
          rekey: null,
          type: [1, 2],
          dataParse: () => ({
            type: 1,
          }),
        },
        'PROJECT_APPROVAL-JURY_DIRECTOR': {
          cmpt: 'approve-director',
          rekey: null,
          type: 1,
        },
        'PROJECT_APPROVAL-PRO': {
          cmpt: 'approve-province',
          rekey: null,
          type: [1, 2],
        },
        psms: {
          cmpt: 'approve-register',
          rekey: null,
          type: [1, 2],
          dataParse: () => ({
            type: 2,
          }),
        },
      }

// 获取审批流程配置信息
export const getApproveProcess = (related, user) => {
  const isUnion = user.scopeArr?.includes('dirGuaPro')
  const pType = +(related.review_method || '')
  const wfKey = getWfKey(isUnion)
  const config = getApproveConfig(isUnion)
  return getApprove(
    {
      wfKey,
      queryId: related.customer_related_id,
      config,
      next: processItem => {
        const { type } = config[processItem.wfn_key] || {}
        if (isDefined(type)) {
          const isMatchType = Array.isArray(type) ? type.includes(pType) : pType === type
          return isMatchType
        }
        return true
      },
    },
    user
  )
}
